const app = getApp()
const request = require('../../../utils/https.js')
import util from '../../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHideToast: false,
    active: 0,
    disabled:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    let userId = wx.getStorageSync('userId');
    //积分消费记录
    request._get(getApp().Api_url + 'rank/entry', {
      userId: userId
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        let info = res.data.data;
        console.log('info==>', info)
        that.setData({
          info: info
         
        })
      }
    }, function () { })
  },
  getMoney(e){
    const that=this;
    let payMoney = e.currentTarget.dataset.paymoney;
    that.setData({
      postMoney: payMoney - that.data.info.growth,
      payMoney: payMoney
    })

  },
  payPost(){
    const that = this;
    let userId = wx.getStorageSync('userId');
    if (that.data.postMoney == 'undefined' || that.data.postMoney == undefined ){
      wx.showToast({
        title: '请选择等级',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    that.setData({
      disabled:true
    })
    request._get(getApp().Api_url + 'profit/freightAppend', {
      userId: userId,
      money: that.data.postMoney
    }, function (res) {
      if (res.data.errCode == 200) {
        console.log('调起支付===>')
        let params = res.data.data;
        wx.requestPayment({
          'appId': params.appId,
          'timeStamp': params.timeStamp + '',
          'nonceStr': params.nonceStr,
          'package': params.packageValue,
          'signType': 'MD5',
          'paySign': params.paySign,
          'success': (payres) => {
            console.log("payres", payres)
            if (payres.errMsg == 'requestPayment:ok') {
              /**
               * 支付成功
               */
              that.onLoad();
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 2000
              })
              that.setData({
                disabled: false
              })
              wx.switchTab({
                url: '/pages/list/list'
              })
              
            }
          },
          'fail': (xhr) => {
            console.log("xhr", xhr)
            if (xhr.errMsg == 'requestPayment:fail cancel') {
              wx.showToast({
                title: '支付失败',
                icon: 'none',
                duration: 2000
              })
              that.setData({
                disabled: false
              })


            }
          }
        })
      }
    }, function () { })
  },
  goShopping() {
    wx.switchTab({
      url: '/pages/list/list',
    })
  },
  //自定义弹框
  onToast() {
    const that = this;
    that.setData({
      isHideToast: true
    })
  },
  shutToast() {
    const that = this;
    that.setData({
      isHideToast: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})